# 全国城市
import time
from string import Template
from threading import Timer

import requests
from bs4 import BeautifulSoup

def timer():
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
        }
        url = 'http://www.dabai28.com/jnd28/1-3.html'
        params = {"show_ram": 1}
        response = requests.get(url, headers=headers)  # 访问url
        listData = []  # 定义数组
        soup = BeautifulSoup(response.text, 'html.parser')  # 获取网页源代码
        tr = soup.find('table').find_all('tr')  # .find定位到所需数据位置  .find_all查找所有的tr（表格）
        previous = soup.find_all('strong')[6].contents
        s = str(previous)
        strip = s.replace('\\t','').replace('\\n','').replace('\\r','').strip().replace('\'','').replace('[','').replace(']','')
        # 去除标签栏
        for j in tr[1:]:
            td = j.find_all('td')  # td表格
            qiHao = td[0].get_text().strip()
            kaiJiang = td[1].get_text().strip()
            teMa = td[2].get_text().strip()
            yuCe = td[3].get_text().strip()
            duiCuo = td[4].get_text()
            listData.append([qiHao, kaiJiang, teMa, yuCe, duiCuo])

        DATA = []
        for one in listData:
            if one[1] == '?':
                DATA.append([one[0], one[1], 0, 0, 0])
                continue
            split = one[1].split('+')
            # print(split)
            # print(split[2][:2])
            # print(split[2][-2:])
            DATA.append([one[0], int(split[0]), int(split[1]), int(split[2][:2]), int(split[2][-2:])])
        # print(listData)  # 打印
        # print(DATA)
        DATA_LEN = len(DATA)
        YU_CE = []
        i = 0
        for one in DATA:
            # if one[1] == '?':
            #     YU_CE.append([0, 0, 0, 0, 0, 0])
            #     i += 1
            #     continue
            if i + 3 == DATA_LEN:
                break
            suanFaOne = DATA[i+1][1] + DATA[i + 2][2] + DATA[i + 3][2] + DATA[i + 3][1] + DATA[i + 2][3]
            suanFaTwo = DATA[i+1][1] + DATA[i + 2][2] + DATA[i + 3][2] + DATA[i + 3][1] + DATA[i + 2][2] + DATA[i+1][3]
            suanFaThree = DATA[i+1][1] + DATA[i + 2][2] + DATA[i + 3][2] + DATA[i+1][3] + DATA[i + 2][1] + DATA[i + 2][3] + DATA[i + 3][2]

            if one[1] == '?':
                YU_CE.append([suanFaOne, suanFaTwo, suanFaThree,-1,-1,-1])
            else:
                YU_CE.append([suanFaOne, suanFaTwo, suanFaThree,
                              1 if one[4] % 2 == suanFaOne % 2 else 0,
                              1 if one[4] % 2 == suanFaTwo % 2 else 0,
                              1 if one[4] % 2 == suanFaThree % 2 else 0])
            i += 1
        # print(YU_CE)
        i = 0
        tds = ''
        for one in DATA:
            if i + 3 == DATA_LEN:
                break
            # td_template = '<tr><td style="text-align:center">${qihao}</td><td style="text-align:center">${kaijiang}</td><td style="text-align:center"><a href="#" class="button ${yucecolor} button-box button-small">${yuce1}</a> <a href="#" class="button ${yucecolor} button-box button-small">${yuce2}</a> <a href="#" class="button ${yucecolor} button-box button-small">${yuce3}</a></td><td style="text-align:center"><a href="#" class="button ${duicuo1color} button-box button-small">${duicuo1}</a> <a href="#" class="button ${duicuo2color} button-box button-small">${duicuo2}</a> <a href="#" class="button ${duicuo3color} button-box button-small">${duicuo3}</a></td></tr>'

            td_template = '<tr><td style="text-align:center">${qihao}</td><td style="text-align:center">${kaijiang}</td><td style="text-align:center"><a href="#" class="button ${duicuo1color} button-box button-small">${yuce1}</a> <a href="#" class="button ${duicuo2color} button-box button-small">${yuce2}</a> <a href="#" class="button ${duicuo3color} button-box button-small">${yuce3}</a></td></tr>'
            template = Template(td_template)
            if YU_CE[i][3] == -1:
                substitute = template.substitute(qihao=one[0], kaijiang=listData[i][1],
                                                 yuce1='双' if YU_CE[i][0] % 2 == 0 else '单',
                                                 yuce2='双' if YU_CE[i][1] % 2 == 0 else '单',
                                                 yuce3='双' if YU_CE[i][2] % 2 == 0 else '单',
                                                 duicuo1='?',
                                                duicuo2='?',
                                                duicuo3='?',
                                                color='button-action',
                                                yucecolor='button-primary',
                                                duicuo1color='button-highlight',
                                                duicuo2color = 'button-highlight',
                                                duicuo3color='button-highlight')
            else:
                substitute = template.substitute(qihao=one[0], kaijiang=listData[i][1], yuce1='双' if YU_CE[i][0] % 2 == 0 else '单',
                                                 yuce2='双' if YU_CE[i][1] % 2 == 0 else '单',
                                                 yuce3='双' if YU_CE[i][2] % 2 == 0 else '单',
                                                 duicuo1=' ' if YU_CE[i][3] == 1  else ' ',
                                                 duicuo2=' ' if YU_CE[i][4] == 1  else ' ',
                                                 duicuo3=' ' if YU_CE[i][5] == 1  else ' ',
                                                 color='button-action',
                                                 yucecolor='button-primary',
                                                 duicuo1color='button-caution' if YU_CE[i][3] == 1 else 'button-action',
                                                 duicuo2color='button-caution' if YU_CE[i][4] == 1 else 'button-action',
                                                 duicuo3color='button-caution' if YU_CE[i][5] == 1 else 'button-action')
            tds += substitute
            i += 1
        # print(tds)
        rate1 = 0
        rate2 = 0
        rate3 = 0
        for one in YU_CE:
            rate1 += one[3]
            rate2 += one[4]
            rate3 += one[5]
        rate1 = rate1 * 100 / len(YU_CE)
        rate2 = rate2 * 100 / len(YU_CE)
        rate3 = rate3 * 100 / len(YU_CE)
        srate = '算法一准确率：%2d%%,算法二准确率：%.2d%%,算法三准确率：%.2d%%' % (rate1, rate2,rate3)+'<br>'+strip
        with open('index.tmpl', 'r', encoding='utf-8') as html:
            read = html.read()
            replace = read.replace('tds', tds).replace('rate', srate).replace('fuck',listData[1][0])
            replace = replace
            with open('index.html', 'w', encoding='utf-8') as write:
                write.write(replace)
        print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        with open('1.dat','w+',encoding='utf-8') as w:
            w.write(listData[1][0])
    except Exception as e:
        print(e)
if __name__ == '__main__':
    while True:
        timer()
        time.sleep(5)